package com.cqut.forum.dao.imp;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.cqut.forum.dao.RelationDao;
import com.cqut.forum.entity.Relation;
import com.cqut.forum.entity.User;

@Component
public class RelationDaoImp extends BaseDaoImp implements RelationDao{
	
	public <T> boolean unDelete(int id) {
		return false;
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getFriends(int userId,int index,int size) {
		String sql = "select u.*,r.relation_id,r.is_care from relation r,user u where r.user_id = ? and u.user_id = r.friend_id " +
				"and r.relation_level=\'好友\'";
		Query query = super.getSqlQuery(sql);
		query.setInteger(0, userId);
		query.setFirstResult(index*size);
		query.setMaxResults(size);
		
		List<Map<String, Object>> list = query.list();
		return list;
	}

	@SuppressWarnings("unchecked")
	public Integer getFriendsSize(int userId) {
		String hql = "select count(*) from Relation r where r.user.userId = "+userId+
			" and r.relationLevel=\'好友\'";
		List<Long> list = super.getHibernateTemplate().find(hql);
		System.out.println((int)((long)list.get(0)));
		return (int)((long)list.get(0));
	}

	@SuppressWarnings("unchecked")
	@Override
	public Relation getFriend(int userId, int friendId) {
		String hql = "select r from Relation r where r.user.userId=? and r.friendId=?";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(userId);
		parameters.add(friendId);
		
		List<Relation> result = (List<Relation>) super.excuteHql(hql, parameters, null);
		return result.size()>0?result.get(0):null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> loadUserByName(String userName) {
		/*String sql = "select u.user_id as userId,u.user_name userName,u.user_photo userPhoto," +
					 "u.user_sex userSex,u.user_place userPlace from user u where u.user_name like \"%"+userName+"%\"";
*/		String sql = "select u.user_id,u.user_name,u.user_photo,u.user_sex,u.user_place from user u where " +
					 "u.user_name like \"%"+userName+"%\"";
		List<Map<String, Object>> result = (List<Map<String, Object>>) super.excuteSql(sql, null, null);
		
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int applyCount(int userId,String level) {
		String sql = "select count(r.relation_id) size from relation r where r.friend_id=? and " +
				"r.relation_level =?";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(userId);
		parameters.add(level);
		
		List<Map<String, Object>> result = (List<Map<String, Object>>) super.excuteSql(sql, parameters, null);
		return ((BigInteger)result.get(0).get("size")).intValue();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> loadByLevel(int userId,String level) {
		String sql = "select u.user_id,u.user_name,u.user_photo,r.relation_id,r.relation_time from " +
				"user u,relation r where r.friend_id=? and r.relation_level=? and r.user_id=u.user_id " +
				"order by r.relation_time asc";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(userId);
		parameters.add(level);
		
		List<Map<String, Object>> result = (List<Map<String, Object>>) super.excuteSql(sql, parameters, null);
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public User getUser(int relationId) {
		String hql = "select r.user from Relation r where r.relationId=?";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(relationId);
		
		List<User> result = (List<User>) super.excuteHql(hql, parameters, null);
		return result.get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public int countFriends(int userId) {
		String sql = "select count(r.user_id) from relation r where r.user_id= ? and r.relation_level=\"好友\"";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(userId);
		
		List<Map<String, BigInteger>> result = (List<Map<String, BigInteger>>) super.excuteSql(sql, parameters, null);
		return (Integer)(result.get(0).get("count(r.user_id)").intValue());
	}

	@SuppressWarnings("unchecked")
	@Override
	public Relation getRelation(int userId, int friendId) {
		String hql = "select r from Relation r where r.user.userId=? and r.friendId=?";
		
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(userId);
		parameters.add(friendId);
		
		List<Relation> result = (List<Relation>) super.excuteHql(hql, parameters, null);
		return result.size()>0?result.get(0):null;
	}
}
